package io.donlon.android.remotecontrol;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

public class BaseApplication extends Application {
    private static BaseApplication sApplication;

    public static BaseApplication getApplication() {
        return sApplication;
    }

    public static Context getAppContext() {
        return getApplication().getApplicationContext();
    }

    public static SharedPreferences getSharedPreferences(String name) {
        return sApplication.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initGlobalDefaultExceptionHandler();
        sApplication = this;
    }

    private void initGlobalDefaultExceptionHandler() {
        Thread.UncaughtExceptionHandler lastHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
            throwable.printStackTrace(); // Set breakpoint here
            if (lastHandler != null) {
                lastHandler.uncaughtException(thread, throwable);
            }
        });
    }
}
